#### Table S14: Norm support by binned affpol (terciles) ####

# Libraries
# library(here)
# library(rio)
# library(tidyverse)
# library(srvyr)
# library(stargazer)

# data_pnas = import(here("Data","data_pnas.rds"))

norm_indc = paste0("norm_",c("judges","polling","censorship","loyalty"),"c")

cs_affpolre_sig = data_pnas |> 
  filter(pid %in% c("Democrat","Republican")) |> # Just D/R
  select(affpolre, all_of(norm_ind), uid, weight) |> 
  pivot_longer(-c("affpolre","uid","weight"), names_to = "var", values_to = "val") |> 
  group_by(var) |>
  nest() |> 
  mutate(mod = map(data, \(x){
    data_svy = as_survey_design(x, ids = uid, weights = weight)
    svyglm(val ~ affpolre, design = data_svy)
  })) |> 
  ungroup() |> 
  mutate(var = recode_factor(var,
                             norm_pollingre = "Polling Stations",
                             norm_loyaltyre = "Loyalty",
                             norm_judgesre = "Ignore Courts",
                             norm_executivere = "Exec. Power",
                             norm_censorshipre = "Censorship")) |> 
  select(-data)

stargazer(pull(cs_affpolre_sig, mod),
          title = "Norm Violation Support by Binned Affective Polarization",
          column.labels = as.character(pull(cs_affpolre_sig, var)),
          dep.var.labels  = "",
          covariate.labels = c("Affpol: 2nd Tercile",
                               "Affpol: 3rd Tercile"),
          notes = "Estimated with survey weights and two-sided tests",
          star.cutoffs = c(0.05, 0.01, 0.001),
          omit.stat = c("aic","bic"),
          ci = T,
          type = "latex",
          out = here("Tables","Supplementary","table_s14.tex"),
          header = F)